import inspect,time

from common import logger, config
from common.NewExcel import Reader,Writer
from web.webkeys import Web

logger.info("开始运行")

def getfunc(obj,method):
    try:
        func = getattr(obj,method)
    except Exception as e:
        return None

    arg = inspect.getfullargspec(func).__str__()
    arg = arg[arg.find("args=")+5:arg.find(", varargs")]
    arg = eval(arg)
    arg.remove("self")
    return func,len(arg)

def runcase(obj, line):
    if len(line[0]) > 0 or len((line[1])) > 0:
        # 分组信息不执行
        return

    func = getfunc(obj, line[3])
    if func is None:
        logger.warn("关键字%s不存在" % line[3])
        return

    if func[1] == 0:
        func[0]()
    elif func[1] == 1:
        func[0](line[4])
    elif func[1] == 2:
        func[0](line[4], line[5])
    elif func[1] == 3:
        func[0](line[4], line[5], line[6])
    else:
        logger.warn("关键字暂不支持超过3个参数")


config.get_config('../lib/conf/conf.properties')

casename = "register"

reader = Reader()
reader.open_excel("../lib/%s.xlsx"% casename)

writer = Writer()
writer.copy_open("../lib/%s.xlsx"% casename,'../lib/result-%s.xlsx' % casename)
sheetname = reader.get_sheets()

# 写开始时间
starttime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
writer.set_sheet(sheetname[0])
writer.write(1, 3, starttime)

obj = Web(writer)


for sheet in sheetname:
    # 设置当前读取的sheet页面
    reader.set_sheet(sheet)
    writer.set_sheet(sheet)
    # for i in range(reader.rows):
    #     obj.row = i
    #     line = reader.readline()
    #     runcase(obj, line)

    # openpyxl
    lines = reader.readline()
    for i in range(reader.rows):
        obj.row = i
        line = lines[i]
        # line = reader.readline()
        runcase(obj, line)

# 写结束时间
endtime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
writer.set_sheet(writer.get_sheets()[0])
writer.write(1, 4, endtime)


writer.save_close()
logger.info("运行结束")

